package com.sx.manage.service.company;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.sx.cache.Cache;
import com.sx.common.util.NumberUtil;
import com.sx.common.util.http.HttpPoolManager;
import com.sx.common.util.http.constants.BasicAccountType;
import com.sx.common.util.http.constants.PaperType;
import com.sx.common.util.json.JackJsonUtil;
import com.sx.common.vo.BaseVO;
import com.sx.manage.biz.consumer.bank.IMConsumerBankMapBiz;
import com.sx.manage.biz.consumer.relation.IMConsumerDataRelationBiz;
import com.sx.manage.listener.MCacheListener;
import com.sx.manage.service.bank.IMBaseConsumerBankMapService;
import com.sx.manage.service.bank.IMConsumerBankMapService;
import com.sx.manage.service.consumer.IMBaseConsumerDataRelationService;
import com.sx.manage.service.sys.IMBaseSystemConfigInfoService;
import com.sx.manage.servlet.ViewAndModel;
import com.sx.manage.util.WebServiceUtil;
import com.sx.manage.util.WebServiceUtils;
import com.sx.model.constants.MDataStatusType;
import com.sx.model.constants.MSystemConstants;
import com.sx.model.constants.type.account.MAccountNumStatus;
import com.sx.model.constants.type.bank.MBindCardStatus;
import com.sx.model.constants.type.bank.MOutCardType;
import com.sx.model.constants.type.bank.MUsingStatus;
import com.sx.model.constants.type.consumer.MConsumerDataRelationType;
import com.sx.model.manage.mybatis.form.bank.MConsumerBankMapForm;
import com.sx.model.manage.mybatis.form.consumer.MCompanyForm;
import com.sx.model.manage.mybatis.form.consumer.MConsumerDataRelationForm;
import com.sx.model.manage.mybatis.form.sys.MSystemConfigInfoForm;
import com.sx.model.manage.mybatis.vo.bank.MPayBankVO;
import com.sx.model.manage.mybatis.vo.biz.center.MAccountVO;
import com.sx.model.manage.mybatis.vo.company.MCompanyOperationInfoVO;
import com.sx.model.manage.mybatis.vo.company.MCompanyVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerBankMapVO;
import com.sx.model.manage.mybatis.vo.consumer.MConsumerDataRelationVO;
import com.sx.model.manage.mybatis.vo.consumer.MVerifOpenAccountVO;
import com.sx.model.manage.mybatis.vo.consumer.login.MConsumerLoginVO;
import com.sx.model.manage.mybatis.vo.result.ResultBooleanVO;
import com.sx.model.manage.mybatis.vo.result.ResultListVO;
import com.sx.model.manage.mybatis.vo.result.ResultObjectVO;
import com.sx.model.manage.mybatis.vo.result.ResultVerifyVO;
import com.sx.model.manage.mybatis.vo.sys.MSysConfigVO;
import com.sx.model.manage.mybatis.web.vo.huaxia0.B2BOpenAccountVO;

/**
 * 公司web端service
 * 
 * @author Administrator
 *
 */
@Service("companyWebServiceImpl")
public class MCompanyWebServiceImpl {
	private Logger log = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "mBaseCompanyServiceImpl")
	private IMBaseCompanyService mBaseCompanyServiceImpl;

	@Resource(name = "consumerBankMapServiceImpl")
	private IMConsumerBankMapService consumerBankMapServiceImpl;

	@Resource(name = "mBaseConsumerBankMapServiceImpl")
	private IMBaseConsumerBankMapService mBaseConsumerBankMapServiceImpl;

	//@Resource(name = "accountService")
	//private IAccountServiceInterface accountService;
	// 用户关系数据
	@Resource(name = "mConsumerDataRelationBizImpl")
	private IMConsumerDataRelationBiz mConsumerDataRelationBizImpl;
	// 用户银行信息biz
	@Resource(name = "mConsumerBankMapBizImpl")
	private IMConsumerBankMapBiz mConsumerBankMapBizImpl;
	// 公司关系数据
	@Resource(name = "mBaseConsumerRelationServiceImpl")
	private IMBaseConsumerDataRelationService mBaseConsumerRelationServiceImpl;
	// 系统参数配置
	@Resource(name = "mBaseSystemConfigInfoServiceImpl")
	private IMBaseSystemConfigInfoService mBaseSystemConfigInfoServiceImpl;
	// 公司扩展信息
	@Resource(name = "mCompanyOperationInfoServiceImpl")
	private IMCompanyOperationInfoService mCompanyOperationInfoServiceImpl;

	@Resource(name = "redisCache")
	private Cache cache;

	@Resource(name = "httppoolmanager")
	private HttpPoolManager httpPoolManager;

	@Resource(name = "webServiceUtils")
	private WebServiceUtils webServiceUtils;

	/**
	 * 获取翻页信息
	 * 
	 * @param form
	 * @return
	 */
	public ResultListVO<MCompanyVO> get_new_company_page(MCompanyForm form) {
		log.info("查询公司翻页信息form={}", form);
		ResultListVO<MCompanyVO> result = mBaseCompanyServiceImpl.get_new_company_page(form);
		if (null != result.getData()) {
			for (MCompanyVO companyVO : result.getData()) {
				MCompanyOperationInfoVO operationVO = mCompanyOperationInfoServiceImpl.queryByCompanyId(companyVO.getId()).getObjVo();
				companyVO.setOperationVO(operationVO);
			}
		}
		return result;
	}

	/**
	 * 公司开通子账户
	 */
	@SuppressWarnings({ "rawtypes" })
	public ResultVerifyVO<MVerifOpenAccountVO> open_account_info(Long[] ids) {
		ResultVerifyVO<MVerifOpenAccountVO> vo = new ResultVerifyVO<>();
		if (null == ids) {
			return null;
		}
		// 非华夏开户
		List<MAccountVO> list_ = new ArrayList<>();
		// 华夏开户
		List<MAccountVO> huaXiaList_ = new ArrayList<>();
		// 华夏银行卡 ‘企业客户号’错误提示
		StringBuilder errorStr = new StringBuilder();
		// 错误信息提示
		List<MVerifOpenAccountVO> errorList = new ArrayList<>();
		for (long i : ids) {
			if (i == 0) {
				continue;
			}
			ResultObjectVO resultVO = mBaseCompanyServiceImpl.search_byIds(new MCompanyForm(i));
			if (null == resultVO.getObjVo()) {
				return new ResultVerifyVO<>("sj.m.company.save.error");
			}
			MCompanyVO companyvo = (MCompanyVO) resultVO.getObjVo();
			MConsumerBankMapVO mbmvo = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(i);
			if (null == mbmvo) {
				return new ResultVerifyVO<>("sj.m.null.num.error");
			}
			MAccountVO accountVO = saveAccountInfo(companyvo, mbmvo);
			// 华夏银行卡
			if (MSystemConstants.HUA_XIA_CLS_BANK_CODE.equals(mbmvo.getCls_bank_code())) {
				huaXiaList_.add(accountVO);
				if (StringUtils.isBlank(mbmvo.getRemark())) {
					log.error(companyvo.getCompany_name());
					MVerifOpenAccountVO openVO = new MVerifOpenAccountVO();
					openVO.setError_info("华夏银行银行卡'企业客户号为必填项'");
					openVO.setName(companyvo.getCompany_name());
					// 加入错误集合
					errorList.add(openVO);
				}
			} else {// 非华夏银行卡
				list_.add(accountVO);
			}
		}
		if (!errorList.isEmpty()) {
			log.error("华夏银行银行卡'企业客户号为必填项'");
			return new ResultVerifyVO<>(false, errorStr.toString(), null, errorList);
		}
		List<MAccountVO> accountList = null;
		log.debug("非华夏银行卡开户size:" + list_.size());
		if (!list_.isEmpty()) {
			accountList = this.openAccount(list_);
			log.info("公司开户==开户已经返回==");
			if (null == accountList) {
				return new ResultVerifyVO<>("sj.m.open.account.error");
			}
			log.debug("开始修改用户银行信息");
			vo = this.openedUpdateBank(accountList);
			if (!vo.isSuccess()) {
				log.error("开户出错提示：" + vo.getMsg());
				return vo;
			}
		}
		// 有错误信息进行合并
		if (null != vo.getData() && !vo.getData().isEmpty()) {
			errorList.addAll(vo.getData());
		}

		log.debug("华夏银行卡开户:" + huaXiaList_.size());
		if (!huaXiaList_.isEmpty()) {
			accountList = this.openAccountHuaxia(huaXiaList_);
			log.info("公司开户==开户已经返回==");
			if (null == accountList) {
				return new ResultVerifyVO<>("sj.m.open.account.error");
			}
			log.debug("开始修改用户银行信息");
			vo = this.openedUpdateBankHuaXia(accountList);
			if (!vo.isSuccess()) {
				log.error("开户出错提示：" + vo.getMsg());
				return vo;
			}
			// 错误信息合并
			if (null != errorList) {
				vo.getData().addAll(errorList);
			}
		}
		log.debug("全部开户流程走完");
		return vo;
	}

	public ResultVerifyVO<MVerifOpenAccountVO> openedUpdateBankHuaXia(List<MAccountVO> accountList) {
		log.debug("开户完成，现在修改用户银行信息表");
		if (null == accountList || accountList.isEmpty()) {
			log.error("开户返回的size为0，或者为空");
			return new ResultVerifyVO<>(false, "开户返回的数据为空", null, new ArrayList<>());
		}
		// 返回界面的result
		ResultVerifyVO<MVerifOpenAccountVO> vo = new ResultVerifyVO<>();
		// 开户结果信息
		List<MVerifOpenAccountVO> list = new ArrayList<>();

		for (MAccountVO info_1 : accountList) {
			// 判断是否开户成功
			if (StringUtils.isBlank(info_1.getErrorInfo()) && StringUtils.isNotBlank(info_1.getAccountNo())) {
				MConsumerBankMapVO mv = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(Long.valueOf(String.valueOf(info_1.getUserId())));
				log.info("公司开户==开户已经返回==开户名称" + info_1.getAccountName() + "开户账号:" + info_1.getAccountNo() + "更新开户状态");
				MConsumerBankMapVO bankMapVO = new MConsumerBankMapVO(mv.getId(), null, info_1.getAccountNo(), MAccountNumStatus.OPENING, null, null, null, null);

				ResultBooleanVO bankResult = consumerBankMapServiceImpl.consumerBankMapUpdate(bankMapVO);
				if (!bankResult.isSuccess()) {
					// 成功 开户信息返回
					vo.setSuccess(false);
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), bankResult.getMsg()));
				} else {
					// 成功 开户信息返回
					vo.setSuccess(true);
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), "开户成功"));
				}

				log.info("公司开户=开户信息(成功)返回 ===开户名称:" + info_1.getAccountName() + "开户信息:" + info_1.getErrorInfo());
			} else {
				// 失败 开户信息返回
				if (null == info_1.getErrorInfo()) {
					log.info("公司开户=开户信息(失败 无结果)返回 ===开户名称:" + info_1.getAccountName() + "开户信息:银行未正确返回数据");
					vo.setSuccess(false);
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), "银行未正确返回数据"));
				} else {
					vo.setSuccess(false);
					log.info("公司开户=开户信息(失败 有结果)返回 ===开户名称:" + info_1.getAccountName() + "开户信息:" + info_1.getErrorInfo());
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), info_1.getErrorInfo()));
				}
			}
			vo.setData(list);
		}
		return vo;
	}

	/**
	 * 华夏银行卡开户
	 * 
	 * @return
	 */
	public List<MAccountVO> openAccountHuaxia(List<MAccountVO> list_) {
		log.debug("华夏银行银行卡开户");
		List<MAccountVO> accountList = null;
		try {
			// 未开户
			accountList = null;// accountService..hxOpenAccounts(list_);
		} catch (Exception e) {
			log.error("开户失败：", e);
		}
		return accountList;
	}

	/**
	 * 开户完成，修改用户银行信息
	 * 
	 * @param accountList
	 * @return
	 */
	public ResultVerifyVO<MVerifOpenAccountVO> openedUpdateBank(List<MAccountVO> accountList) {
		log.debug("开户完成，现在修改用户银行信息表");
		if (null == accountList || accountList.isEmpty()) {
			return new ResultVerifyVO<>(false, "开户返回的数据为空", null, null);
		}
		// 返回界面的result
		ResultVerifyVO<MVerifOpenAccountVO> vo = new ResultVerifyVO<>();
		// 开户结果信息
		List<MVerifOpenAccountVO> list = new ArrayList<>();

		for (MAccountVO info_1 : accountList) {
			// 判断是否开户成功
			if (null != info_1.getBankAccountNo()) {
				MConsumerBankMapVO mv = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(Long.valueOf(String.valueOf(info_1.getUserId())));
				log.info("公司开户==开户已经返回==开户名称" + info_1.getAccountName() + "开户账号:" + info_1.getAccountNo() + "更新开户状态");
				ResultBooleanVO bankResult = consumerBankMapServiceImpl.consumerBankMapUpdate(new MConsumerBankMapVO(mv.getId(), Long.valueOf(String.valueOf(info_1.getUserId())), info_1.getAccountNo(), MAccountNumStatus.OPENED, info_1.getRelaAcct(),
						MSystemConstants.CONSUMER_NATURE_COMPANY, new Date(), info_1.getBankAccountNo()));

				if (!bankResult.isSuccess()) {
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), bankResult.getMsg()));
				} else {
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), "开户成功"));
				}
				// 成功 开户信息返回
				vo.setSuccess(true);
				list.add(new MVerifOpenAccountVO(info_1.getAccountName(), "开户成功"));
				log.info("公司开户=开户信息(成功)返回 ===开户名称:" + info_1.getAccountName() + "开户信息:" + info_1.getErrorInfo());
			} else {
				// 失败 开户信息返回
				if (null == info_1.getErrorInfo()) {
					log.info("公司开户=开户信息(失败 无结果)返回 ===开户名称:" + info_1.getAccountName() + "开户信息:银行未正确返回数据");
					vo.setSuccess(false);
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), "银行未正确返回数据"));
				} else {
					vo.setSuccess(false);
					log.info("公司开户=开户信息(失败 有结果)返回 ===开户名称:" + info_1.getAccountName() + "开户信息:" + info_1.getErrorInfo());
					list.add(new MVerifOpenAccountVO(info_1.getAccountName(), info_1.getErrorInfo()));
				}
			}
			vo.setData(list);
		}
		return vo;
	}

	/**
	 * 非华夏银行卡开户
	 * 
	 * @return
	 */
	public List<MAccountVO> openAccount(List<MAccountVO> list_) {
		log.debug("非华夏银行银行卡开户");
		List<MAccountVO> accountList = new ArrayList<>(list_.size());

		for (MAccountVO accountVO : list_) {
			List<MAccountVO> voList = new ArrayList<>(1);
			voList.add(accountVO);
			try {
				// 未完成
				List<MAccountVO> returnList = null;// accountService.openAccounts(voList);
				accountList.addAll(returnList);
			} catch (Exception e) {
				log.error("开户失败：", e);
				MAccountVO accountVO_ = new MAccountVO();
				accountVO_.setErrorInfo("调用出错：" + e.getMessage());
				accountList.add(accountVO_);
			}
		}
		return accountList;
	}

	/**
	 * 封装 AccountInfo 对象
	 * 
	 * @param company
	 * @param userBankMap
	 * @param paperType
	 * @return
	 */
	public MAccountVO saveAccountInfo(MCompanyVO companyvo, MConsumerBankMapVO userBankMap) {
		if (null == companyvo) {
			return null;
		}
		if (null == userBankMap) {
			return null;
		}
		MAccountVO accountInfo = new MAccountVO();
		accountInfo.setUserId(companyvo.getId());
		accountInfo.setBankCode(userBankMap.getBank_code());// companyvo.getUserBankMap().getBank_info().getBank_code()
		accountInfo.setAccountName(userBankMap.getBank_consumer_name());
		accountInfo.setMobile(companyvo.getTelephone());
		accountInfo.setAccountType(BasicAccountType.COMPANY);//
		accountInfo.setPaperType(companyvo.getPapers_type());
		accountInfo.setPaperNo(companyvo.getOrg_code());// 身份证号公司的暂时用组织机构代码getOrg_code,getPapers_num
		accountInfo.setRelaAcct(userBankMap.getCard_num());
		accountInfo.setRelaAcctName(userBankMap.getBank_consumer_name());
		accountInfo.setRelaAcctBank(userBankMap.getCls_bank_name());
		accountInfo.setRelaAcctBankAddr(userBankMap.getBank_name());// companyvo.getUserBankMap().getBankInfo().getBank_name()
		accountInfo.setRelaAcctBankCode(userBankMap.getBank_code()); // companyvo.getUserBankMap().getBankInfo().getBank_code()
		accountInfo.setEmail(companyvo.getEmail());
		accountInfo.setZipCode(companyvo.getPostal_code());
		accountInfo.setLawName(companyvo.getLegal_person());
		accountInfo.setLinkMan(companyvo.getContacts());
		accountInfo.setPhone(companyvo.getTelephone());
		accountInfo.setAddr(companyvo.getCompany_address());
		accountInfo.setClsBankName(userBankMap.getCls_bank_name());
		accountInfo.setClsBankCode(userBankMap.getCls_bank_code());
		accountInfo.setRemark(userBankMap.getRemark());
		log.info("==================公司开户ID开户全部信息={}", JackJsonUtil.objToJson(accountInfo));
		return accountInfo;
	}

	/**
	 * 转换 证件类型
	 * 
	 * @param papersType
	 * @return
	 */
	public static PaperType changePaperType(String papersType) {

		PaperType pt = null;

		// IDENTITYCARD 身份证 identitycard
		if (papersType.equals("identitycard")) {
			pt = PaperType.IDENTITYCARD;
			// SOLDIERCARD 军官证
		} else if (papersType.equals("police")) {

			pt = PaperType.SOLDIERCARD;
			// FAMILYREGISTER 户口本
		} else if (papersType.equals("household")) {

			pt = PaperType.HOUSEHOLD;
			// PASSPORT 护照
		} else if (papersType.equals("passport")) {
			pt = PaperType.PASSPORT;
			// TEMPPAPER 临时身份证
		} else if (papersType.equals("temporary")) {
			pt = PaperType.TEMPORARY;
			// OTHER 其他
		} else {
			pt = PaperType.OTHER;
		}
		return pt;
	}

	/**
	 * 封装PaperType
	 * 
	 * @param company
	 * @param paperType
	 * @return
	 */
	@SuppressWarnings("static-access")
	public PaperType changePaperType(MCompanyVO company) {
		PaperType paperType = null;
		/*** 证件类型：身份证 ***/
		if (company.getPapers_type().equals(PaperType.IDENTITYCARD)) {
			paperType = PaperType.IDENTITYCARD;
			/*** 证件类型：军官，警官证 ***/
		} else if (company.getPapers_type().equals(PaperType.SOLDIERCARD)) {
			paperType = PaperType.SOLDIERCARD;
			/*** 证件类型：户口本 ***/
		} else if (company.getPapers_type().equals(PaperType.HOUSEHOLD)) {
			paperType = PaperType.HOUSEHOLD;
			/*** 证件类型：临时证件 ***/
		} else if (company.getPapers_type().equals(PaperType.TEMPORARY)) {
			paperType = PaperType.TEMPORARY;
			/*** 证件类型：护照 ***/
		} else if (company.getPapers_type().equals(PaperType.PASSPORT)) {
			paperType = PaperType.PASSPORT;

		} else {
			paperType = PaperType.OTHER;
		}
		return paperType;
	}

	/**
	 * 封装 MAccountVO 对象
	 * 
	 * @param company
	 * @param userBankMap
	 * @param paperType
	 * @return
	 */
	public MAccountVO createAccountInfo(MCompanyVO company, MConsumerBankMapVO userBankMap, PaperType paperType) {
		if (null == company && null == userBankMap) {
			return null;
		}
		MAccountVO info = new MAccountVO();
		info.setUserId(company.getId());
		info.setBankCode(userBankMap.getBank_code());
		info.setAccountName(userBankMap.getBank_consumer_name());
		info.setMobile(company.getTelephone());
		info.setBasicAccountType(BasicAccountType.COMPANY);
		info.setPaperType(paperType);
		info.setPaperNo(company.getPapers_num());
		info.setRelaAcct(userBankMap.getCard_num());
		info.setRelaAcctName(userBankMap.getBank_consumer_name());
		info.setRelaAcctBank(userBankMap.getCls_bank_name());
		info.setRelaAcctBankAddr(userBankMap.getBank_name());
		info.setRelaAcctBankCode(userBankMap.getBank_code());
		info.setEmail(company.getEmail());
		info.setZipCode(company.getPostal_code());
		info.setLawName(company.getLegal_person());
		info.setLinkMan(company.getContacts());
		info.setPhone(company.getTelephone());
		info.setAddr(company.getCompany_address());
		info.setCreateTime(new Date());
		info.setClsBankName(userBankMap.getCls_bank_name());
		info.setClsBankCode(userBankMap.getCls_bank_code());
		log.info("==================公司开户ID开户全部信息:" + "companyId=" + company.getId() + "basicAccountType=" + BasicAccountType.COMPANY + "accountName=" + userBankMap.getBank_consumer_name() + "paperType=" + paperType + "paperNo="
				+ company.getPapers_num() + "relaAcct=" + userBankMap.getCard_num() + "relaAcctName=" + userBankMap.getBank_consumer_name() + "relaAcctBank=" + userBankMap.getCls_bank_name() + "relaAcctBankAddr=" + userBankMap.getProvince_name()
				+ userBankMap.getCity_name() + userBankMap.getBank_name() + "relaAcctBankCode=" + userBankMap.getBank_code() + "email=" + company.getEmail() + "mobile" + company.getTelephone() + "clsBankName:" + userBankMap.getCls_bank_name()
				+ "clsBankCode:" + userBankMap.getCls_bank_code());
		return info;
	}

	/**
	 * 保存公司银行信息对应关系
	 * 
	 * @param userBankMapVO
	 * @return
	 */
	public ResultBooleanVO insertBankNotDefault(MConsumerBankMapForm bankMapForm, MConsumerLoginVO loginConsumer) {
		log.info("新增公司银行卡参数为空 bankMapForm={},loginConsumer={},", bankMapForm, loginConsumer);
		if (null == bankMapForm || null == bankMapForm.getConsumer_id() || null == loginConsumer || StringUtils.isBlank(bankMapForm.getChannel_type())) {
			log.info("新增公司银行卡参数为空 bankMapForm={},loginConsumer={},", bankMapForm, loginConsumer);
			return new ResultBooleanVO("m.parameter.is.null");
		}
		MConsumerBankMapForm queryForm = new MConsumerBankMapForm();
		queryForm.setConsumer_id(bankMapForm.getConsumer_id());
		queryForm.setCard_num(bankMapForm.getCard_num());
		queryForm.setChannel_type(bankMapForm.getChannel_type());
		List<MConsumerBankMapVO> bankMapVOList = this.mBaseConsumerBankMapServiceImpl.query_consumer_bank_list(queryForm).getData();
		if (null != bankMapVOList && !bankMapVOList.isEmpty()) {
			log.info("公司已经绑定了此张银行卡，不需要重复绑定queryForm={}", queryForm);
			return new ResultBooleanVO(false, null, "公司已经绑定了此张银行卡，不需要重复绑定!");
		}
		bankMapForm.setAccount_type(MSystemConstants.CONSUMER_NATURE_COMPANY);
		bankMapForm.setStatus(MUsingStatus.CAN_USED);
		bankMapForm.setConsumer_type(MSystemConstants.CONSUMER_NATURE_COMPANY);

		String usedChannelType = mBaseConsumerRelationServiceImpl.queryCompanyChannelType(bankMapForm.getConsumer_id());
		// 新增的银行卡不是在用的渠道，
		if (!bankMapForm.getChannel_type().equals(usedChannelType)) {
			bankMapForm.setChannel_status(MDataStatusType.DATA_DISABLE);
		} else {// 新增的银行卡 是在用的渠道，
			bankMapForm.setChannel_status(MDataStatusType.DATA_AVAILABLE);
		}
		// 数据入库
		log.info("保存公司银行信息form={}", bankMapForm);
		return this.consumerBankMapServiceImpl.insertNotDefault(bankMapForm);
	}

	/**
	 * 开通华夏交易会员代码(华夏二代接口开户)
	 */
	public ResultVerifyVO<MVerifOpenAccountVO> accountInfoHuaxia02(Long[] ids) {
		log.debug("华夏二代接口开户 ids={}", NumberUtil.arrayToStr(ids));
		if (null == ids || 0 == ids.length) {
			log.info("公司开户失败，ids为空");
			return new ResultVerifyVO<>();
		}
		// 查询所有总行信息
		Map<String, MPayBankVO> payMap = MCacheListener.getProvinceMap();
		StringBuilder error = new StringBuilder();
		// 查询用户信息
		for (Long companyId : ids) {
			log.info("公司开户查询公司基本信息companyId={}", companyId);
			MCompanyVO companyVO = mBaseCompanyServiceImpl.search_byIds(new MCompanyForm(companyId)).getObjVo();
			if (null != companyVO) {
				// 查看是否使用过华夏二代开户
				log.info("判断公司是否开过户companyId={}", companyId);
				if (isOpeanAccountHX02(companyId).isSuccess()) {
					log.info("公司开户失败，账户信息已经存在companyId={}", companyId);
					error.append(companyVO.getCompany_name()).append("已经开通华夏交易会员代码").append("\n");
					continue;
				}
				// 调用开户接口
				log.info("调用开户接口companyId={}", companyId);
				ResultObjectVO<B2BOpenAccountVO> tradeMemCodeResult = opeanAccountHX02(companyVO, payMap, MSystemConstants.CHANNEL_TYPE_HXBV2);// 调用开户接口
				if (!tradeMemCodeResult.isSuccess()) {
					error.append(companyVO.getCompany_name()).append(tradeMemCodeResult.getMsg()).append("\n");
					log.info("调用开户接口失败companyId={},", companyId, tradeMemCodeResult.getMsg());
					continue;
				}
				B2BOpenAccountVO accountVO = tradeMemCodeResult.getObjVo();
				// 开户信息入库
				log.info("公司开户信息用户关系库accountVO={},companyId={}", accountVO, companyId);
				ResultBooleanVO result = mConsumerDataRelationBizImpl.insertOpenAccountVOForCompany(accountVO, companyId);
				if (!result.isSuccess()) {
					log.info("公司开户信息用户关系库companyId={},error={}", companyId, result.getMsg());
					error.append(companyVO.getCompany_name()).append("开户成功，入库错误").append("\n");
				}
				// 扩展信息入库
				log.info("修改公司信息扩展库数据OPENED:companyId={}", companyId);
				result = mCompanyOperationInfoServiceImpl.updateHuaxia02AccountStatus(companyVO.getId(), MAccountNumStatus.OPENED);
				if (!result.isSuccess()) {
					log.info("修改公司信息扩展库失败:error={}", result.getMsg());
					error.append(companyVO.getCompany_name()).append("开户成功，入扩展信息库错误").append("\n");
				}
				// 调用支付中心
				BaseVO baseVO = null;
				try {
					log.info("通知公司段开户信息accountNo={},bankAccountNo={},companyId={}", accountVO.getAccountNo(), accountVO.getBankAccountNo(), companyId);
					//baseVO = accountService.createAccount(accountVO.getAccountNo(), accountVO.getBankAccountNo(), String.valueOf(companyId));
					if (!baseVO.isSuccess()) {
						log.info("通知公司段开户信息companyId={},error={}", companyId, baseVO.getMsg());
						error.append(companyVO.getCompany_name()).append("开户,入库成功，通知支付中心异常").append("\n");
					}
				} catch (Exception e) {
					log.error("华夏二期开通子账户，通知支付中心异常error={}", e);
					error.append(companyVO.getCompany_name()).append("开户,入库成功，通知支付中心异常").append("\n");
				}

			}
		}
		ResultVerifyVO result = new ResultVerifyVO<>();
		if (error.length() > 0) {
			result.setSuccess(false);
			log.error("公司开户异常error={}", error.toString());
			result.setMsg(error.toString());
		}
		return result;
	}

	/** 开通华夏子账户（单个） **/
	private ResultObjectVO<B2BOpenAccountVO> opeanAccountHX02(MCompanyVO companyVO, Map<String, MPayBankVO> payMap, String channelName) {
		log.info("开通华夏子账户channelName={},companyVO={}", channelName, companyVO);
		if (StringUtils.isBlank(channelName) && !MSystemConstants.CHANNEL_TYPE_HXBV2.equals(channelName)) {
			log.info("companyVO={}不是华夏二代渠道，不能开通华夏二代子账户", companyVO);
			return new ResultObjectVO<>("sx.consumer.open.account.channel.type.is.error");
		}
		// 组织Map类型
		MConsumerBankMapVO bankMapVO = this.mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(companyVO.getId());
		if (null == bankMapVO || null == bankMapVO.getId() || StringUtils.isBlank(bankMapVO.getCls_bank_name())) {
			log.info("公司没有银行卡信息companyVO={}", companyVO);
			return new ResultObjectVO<>(false, null, "公司没有默认的银行卡，请设置");
		}
		MPayBankVO payVO = payMap.get(bankMapVO.getCls_bank_name());
		if (null == payVO) {
			log.info("系统不支持公司银行卡的总行，如果总行为新增数据，请重启系统", companyVO);
			return new ResultObjectVO<>(false, null, "系统不支持公司银行卡的总行，如果总行为新增数据，请重启系统");
		}
		bankMapVO.setCls_bank_name(payVO.getHuaxia_bank());

		if (null == bankMapVO || StringUtils.isBlank(bankMapVO.getCard_num())) {
			log.info(" 公司没有设置注入银行卡 networkVO={}", bankMapVO);
			return new ResultObjectVO<>("m.user.status.bank.info.cardnum.is.null");
		}
		// 生产开户类
		//OpenAccountForm form = ClassPackUtil.createOpenAccountFormForCompany(companyVO, bankMapVO, cache, channelName);

//		log.info("生成开通交易会员代码form ={}", form);
//		try {
//			HttpResult httpResult = httpPoolManager.getHttpClient().postAuthorization(WebServiceUtil.huaxia02TradeUrl(), ContentType.JSON, null, MConsumerWebServiceImpl.HTTP_TIME_OUT, JackJsonUtil.objToJson(form), "UTF-8");
//			if (null != httpResult && 200 == httpResult.getStatus()) {
//				log.info("返回开通交易会员代码结果 httpResult={}", httpResult.getHttpresult());
//				B2BOpenAccountVO accountVO = JackJsonUtil.jsonToJavaBean(httpResult.getHttpresult(), B2BOpenAccountVO.class);
//				if (accountVO.isSuccess()) {
//					accountVO.setAccountNo(form.getAccountNo());
//					accountVO.setBankAccountNo(form.getBankAccountNo());
//					return new ResultObjectVO<>(accountVO);
//				} else {
//					return new ResultObjectVO<>(false, null, accountVO.getMsg());
//				}
//			}
//		} catch (Exception e) {
//			log.error("开通交易会员代码http异常error={}", e);
//		}
		return new ResultObjectVO<>("sx.consumer.bank.trade.mem.code.open.fail");

	}

	/** 判断公司是否开通交易会员代码 **/
	public ResultBooleanVO isOpeanAccountHX02(Long consumer_id) {
		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(consumer_id, MConsumerDataRelationType.COMPANY_MEMBER_CODE_HUAXIA_02).getObjVo();
		if (null != relationVO) {
			return new ResultBooleanVO();
		} else {
			return new ResultBooleanVO(false, null, null);
		}
	}

	/** 交易会员代码入库 **/
	private ResultBooleanVO insertTradeMemCode(MVerifOpenAccountVO accountVO, Long consumer_id, MConsumerDataRelationType tradeMemCodeType, String channelType) {
		return mConsumerDataRelationBizImpl.insertTradeMemCode(null, consumer_id, MConsumerDataRelationType.CONSUMER_COMPANY_CHANNEL_TYPE, tradeMemCodeType, channelType);
	}

	/** 公司切换使用的渠道类型 **/
	@SuppressWarnings("rawtypes")
	public ResultBooleanVO changeTradeMemCode(Long companyId, String relationNum) {
		log.info("公司切换使用的渠道类型companyId={},relationNum={}", companyId, relationNum);
		// 复制数据
		ResultBooleanVO result = mConsumerBankMapBizImpl.companyChangeChannelCopyBankInfo(companyId, relationNum);
		if (!result.isSuccess()) {
			log.error("公司切换使用的渠道类型-复制公司银行卡信息-失败companyId={},relationNum={}", companyId, relationNum);
			return result;
		}
		// 通知公司端 userId, accountNo, bankAccountNo, bankcode
		BaseVO baseVO = new BaseVO();// accountService.accountChangeChannel(String.valueOf(consumerId),
										// bankMapVO.getAccount_no(),
										// bankMapVO.getBank_account_no(),
										// relationNum);
		if (!baseVO.isSuccess()) {
			log.info("公司切换渠道失败companyId={},relationNum={}", companyId, relationNum);
			return new ResultBooleanVO("sx.consumer.change.channel.type.error");
		}
		// 切换渠道
		result = mConsumerDataRelationBizImpl.changeTradeMemCode(companyId, relationNum, MConsumerDataRelationType.CONSUMER_COMPANY_CHANNEL_TYPE);
		if (!result.isSuccess()) {
			log.info("公司切换渠道类型失败companyId={},relationNum={}", companyId, relationNum);
			// 切换失败,通知公司端，切换回原来的数据
			// 获取原来的渠道
			MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_company_id_and_relation_type(companyId, MConsumerDataRelationType.CONSUMER_COMPANY_CHANNEL_TYPE).getObjVo();
			// 获取原来的银行信息
			MConsumerBankMapVO usedBankMapVO = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(companyId, relationNum);
			// 通知公司端(未完成)
			// accountService.accountChangeChannel(String.valueOf(companyId),
			// usedBankMapVO.getAccount_no(),
			// usedBankMapVO.getBank_account_no(),
			// relationVO.getRelation_string_num());
			return result;
		}
		return result;
	}

	/** 跳转到切换渠道界面 */
	@SuppressWarnings("rawtypes")
	public void changeTypeView(ViewAndModel model, Long companyId) {
		log.info("跳转到切换渠道界面companyId={}", companyId);
		model.setViewName("");
		Boolean canChange = null;
		// 判断能否切换渠道
		if (null == companyId) {
			canChange = false;
		} else {
			//BaseVO baseVO = accountService.valAccountForChange(String.valueOf(companyId));
			//canChange = baseVO.isSuccess();
		}
		model.addObject("canChange", canChange);
		// 获取能够切换的数据
		log.error("获取能够切换的数据companyId={}", companyId);
		MConsumerDataRelationForm relationForm = new MConsumerDataRelationForm();
		relationForm.setConsumer_id(companyId);
		relationForm.setRelation_type(MConsumerDataRelationType.CONSUMER_COMPANY_CHANNEL_TYPE);
		ResultListVO<MConsumerDataRelationVO> listResult = mConsumerDataRelationBizImpl.queryList(relationForm);

		List<MConsumerDataRelationVO> canChangeList = new ArrayList<>();
		for (MConsumerDataRelationVO relationVO : listResult.getData()) {
			// 判断渠道是否设置工资卡，没有设置工资卡（默认使用的卡）的不能切换
			MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(companyId, relationVO.getRelation_string_num());
			if (null != bankMapVO) {
				canChangeList.add(relationVO);
			}
		}
		model.addObject("canChangeList", canChangeList);
	}

	/** 跳转到开通子账户界面 */
	public void openAccountView(ViewAndModel model, Long companyId, String channelType) {
		log.info("跳转到开通子账户界面companyId={}", companyId);
		model.setViewName("");
		// 查询用户 channelType 下的所有 银行信息
		ResultListVO<MConsumerBankMapVO> resultList = mBaseConsumerBankMapServiceImpl.query_bank_list_by_consumer_id(companyId, channelType);

		List<MConsumerBankMapVO> bankMapList = new ArrayList<>();
		for (MConsumerBankMapVO bankMapVO : resultList.getData()) {
			// 未开户的加入集合
			if (StringUtils.isBlank(bankMapVO.getAccount_no()) && StringUtils.isBlank(bankMapVO.getBank_account_no())) {
				bankMapList.add(bankMapVO);
			}
		}
		// 数据带入界面
		model.addObject("bankMapList", bankMapList);
	}

	/** 开通子账户 **/
	public ResultBooleanVO openHx2AccountNo(String tradeMemCode, Long bankMapId) {
		if (StringUtils.isBlank(tradeMemCode) || null == bankMapId) {
			return new ResultBooleanVO("m.parameter.is.null");
		}
		MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumer_bank_map_by_id(bankMapId).getObjVo();
		if (null == bankMapVO) {
			return new ResultBooleanVO("m.user.status.bank.info.cardnum.is.null");
		}
		if (!StringUtils.isBlank(bankMapVO.getAccount_no())) {
			log.info("已经开通子账户bankMapId={}", bankMapId);
			return new ResultBooleanVO("sx.consumer.bank.account.is.exists");
		}
		// （未完成）调用公司
		String account_no = "rwerwe123456";
		String bank_account_no = "123456";
		// 更新数据库 accountNo,bankAccountNo
		MConsumerBankMapForm bankMapForm = new MConsumerBankMapForm();
		bankMapForm.setId(bankMapVO.getId());
		bankMapForm.setAccount_no(account_no);
		bankMapForm.setBank_account_no(bank_account_no);
		bankMapForm.setAccount_no_status(MAccountNumStatus.OPENED);
		bankMapForm.setOpen_account_time(new Date());
		bankMapForm.setTrade_mem_code(tradeMemCode);

		return consumerBankMapServiceImpl.consumerBankMapUpdate(bankMapForm);
	}

	/** 添加界面 **/
	public ViewAndModel add_viewCompany(ViewAndModel viewAndModel) {
		Long company_code = this.mBaseCompanyServiceImpl.getCompanyCode();

		MSystemConfigInfoForm form = new MSystemConfigInfoForm();
		form.setConfig_type(MSystemConstants.OPEN_ACCOUNT_CHANNEL_TYPES);
		List<MSysConfigVO> type_list = mBaseSystemConfigInfoServiceImpl.querySystemConfigList(form).getData();
		viewAndModel.addObject("type_list", type_list);

		MCompanyVO mCompanyVO = new MCompanyVO();
		mCompanyVO.setCompany_code(company_code);
		viewAndModel.addObject("mCompanyVO", mCompanyVO);
		viewAndModel.setViewName("company/companyadd");
		return viewAndModel;
	}

	/** 编辑界面 **/
	public ViewAndModel query_view_company(ViewAndModel viewAndModel, MCompanyForm form) {
		log.debug("添加系统操作日志：MCompanyController===search_SysIntroduce==formJson=" + JackJsonUtil.objToJson(form));
		viewAndModel.setViewName("company/companyedit");
		// 查询公司信息
		ResultObjectVO<MCompanyVO> resultObjectVO = mBaseCompanyServiceImpl.search_by_bank_Ids(form);
		MCompanyVO mCompanyVO = resultObjectVO.getObjVo();
		viewAndModel.addObject("mCompanyVO", mCompanyVO);
		// 查询渠道列表
		MSystemConfigInfoForm queryForm = new MSystemConfigInfoForm();
		queryForm.setConfig_type(MSystemConstants.OPEN_ACCOUNT_CHANNEL_TYPES);
		List<MSysConfigVO> type_list = mBaseSystemConfigInfoServiceImpl.querySystemConfigList(queryForm).getData();
		viewAndModel.addObject("type_list", type_list);
		// 开通的子账号
		MConsumerDataRelationVO relationVO = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(form.getId(), MConsumerDataRelationType.C_BANK_ACCOUNT_CODE_02).getObjVo();
		if (null != relationVO) {
			viewAndModel.addObject("bank_account_no", relationVO.getRelation_string_num());
		}
		// 交易会员代码
		MConsumerDataRelationVO tradeMemCode = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(form.getId(), MConsumerDataRelationType.COMPANY_MEMBER_CODE_HUAXIA_02).getObjVo();
		if (null != tradeMemCode) {
			viewAndModel.addObject("tradeMemCode", tradeMemCode.getRelation_string_num());
		}
		// 交易会员代码
		MConsumerDataRelationVO accoutNo = mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(form.getId(), MConsumerDataRelationType.C_ACCOUNT_CODE_02).getObjVo();
		if (null != accoutNo) {
			viewAndModel.addObject("account_no", accoutNo.getRelation_string_num());
		}
		// 查询银行信息
		MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumerBankMap_for_manage(mCompanyVO.getId(), null);
		// 银行信息不为空
		if (null != bankMapVO && StringUtils.isNotBlank(bankMapVO.getChannel_type())) {
			MSystemConfigInfoForm configForm = new MSystemConfigInfoForm();
			configForm.setConfig_value(bankMapVO.getChannel_type());
			configForm.setConfig_type(MSystemConstants.CONFIG_TYPE_OPEN_ACCOUNT_CHANNEL_TYPES);
			List<MSysConfigVO> configList = mBaseSystemConfigInfoServiceImpl.search_systemConfig(configForm).getData();
			bankMapVO.setChannel_type_name(configList.get(0).getConfig_desc());
			viewAndModel.addObject("userBankMap", bankMapVO);
		}

		return viewAndModel;
	}

	/**
	 * 跳转到用户银行信息添加界面
	 */
	public ViewAndModel bindBankCardView(ViewAndModel model, Long company_id) {
		log.debug("跳转到用户银行信息添加界面company_id={}", company_id);
		model.setViewName("company/bank/consumerBankList");

		// 获取默认的开户渠道
		MConsumerDataRelationVO relationVO = this.mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(company_id, MConsumerDataRelationType.CONSUMER_COMPANY_CHANNEL_TYPE).getObjVo();
		model.addObject("channel_type", relationVO);
		model.addObject("company_id", company_id);
		return model;
	}

	/**
	 * 跳转到用户银行信息添加界面
	 */
	public ResultListVO<MConsumerBankMapVO> bindBankCardList(Long company_id) {
		log.debug("跳转到用户银行信息添加界面company_id={}", company_id);
		// 获取默认的开户渠道
		MConsumerDataRelationVO relationVO = this.mConsumerDataRelationBizImpl.query_by_consumer_id_and_relation_type(company_id, MConsumerDataRelationType.CONSUMER_COMPANY_CHANNEL_TYPE).getObjVo();
		// 获取用户所有银行卡信息
		if (null != relationVO) {
			ResultListVO<MConsumerBankMapVO> result = this.mBaseConsumerBankMapServiceImpl.query_bank_list_by_consumer_id(company_id, relationVO.getRelation_string_num());
			Long num = Long.valueOf(0);
			if (null != result.getData()) {
				num = Long.valueOf(result.getData().size());
			}
			result.setRecordsTotal(num);
			result.setRecordsFiltered(num);
			return result;
		} else {
			return new ResultListVO<>(new ArrayList<>(0), Long.valueOf(0), Long.valueOf(0));
		}
	}

	// 解除出入金卡
	public ResultBooleanVO relieveBankCard(Long company_id) {
		// 获取银行信息
		MConsumerBankMapVO bankMapVO = this.mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(company_id);
		// 解除出入金卡
		ResultBooleanVO result = relieveBankCardOper(company_id, bankMapVO);
		if (!result.isSuccess()) {
			return result;
		}
		// 扩展信息入库
		result = mCompanyOperationInfoServiceImpl.updateHuaxia02BindCardStatus(company_id, MBindCardStatus.RELIEVE);
		if (!result.isSuccess()) {
			return result;
		}
		return relieveCardBindSuccess(company_id, bankMapVO);
	}

	private ResultBooleanVO relieveCardBindSuccess(Long consumer_id, MConsumerBankMapVO bankMapVO) {
		// 查询银行信息
		MConsumerBankMapForm bankMapForm = new MConsumerBankMapForm();
		bankMapForm.setId(bankMapVO.getId());
		bankMapForm.setAccount_no(null);
		bankMapForm.setBank_account_no(null);
		bankMapForm.setAccount_no_status(MAccountNumStatus.NOT_OPEN);
		bankMapForm.setTrade_mem_code(null);
		bankMapForm.setStatus(MUsingStatus.DEFAULT_USED);
		bankMapForm.setChannel_status(MDataStatusType.DATA_AVAILABLE);
		bankMapForm.setOut_card_type(null);
		// 修改银行信息
		return consumerBankMapServiceImpl.consumerBankMapUpdateForNull(bankMapForm);
	}

	// 绑定出入金卡
	public ResultBooleanVO bindBankCard(Long company_id, Long bank_map_id) {
		// 判断用户能否绑卡
		ResultBooleanVO result = bindBankCardValidate(company_id, bank_map_id);
		if (!result.isSuccess()) {
			return result;
		}
		// 绑卡
		result = bindBankCardOper(company_id, bank_map_id);
		if (!result.isSuccess()) {
			return result;
		}
		// 扩展信息入库
		result = mCompanyOperationInfoServiceImpl.updateHuaxia02BindCardStatus(company_id, MBindCardStatus.BIND);
		if (!result.isSuccess()) {
			return result;
		}
		// 子账号、摊位号信息 进入有银行信息表
		return bankCardBindSuccess(company_id, bank_map_id);
	}

	/** 绑卡成功后续操作 **/
	private ResultBooleanVO bankCardBindSuccess(Long company_id, Long bank_map_id) {
		// 查询银行信息
		MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumer_bank_map_by_id(bank_map_id).getObjVo();
		MConsumerBankMapForm bankMapForm = new MConsumerBankMapForm();
		bankMapForm.setId(bank_map_id);
		bankMapForm.setAccount_no(webServiceUtils.queryAccountNoCompany(company_id));
		bankMapForm.setBank_account_no(webServiceUtils.queryBranchAccountNoCompany(company_id));
		bankMapForm.setAccount_no_status(MAccountNumStatus.OPENED);
		bankMapForm.setTrade_mem_code(webServiceUtils.queryTradeMemberCodeCompany(company_id));
		bankMapForm.setOut_card_type(MOutCardType.IN_OUT_CARD);
		// 是华夏银行卡，（需要第二天华夏人员确认后才能使用）
		if (WebServiceUtil.configHuaXiaBank(bankMapVO.getCls_bank_name())) {
			bankMapForm.setStatus(MUsingStatus.DEFAULT_USED_WAIT);
		} else {
			bankMapForm.setStatus(MUsingStatus.DEFAULT_USED);
		}
		// 修改银行信息
		return consumerBankMapServiceImpl.consumerBankMapUpdate(bankMapForm);
	}

	// 绑卡前的验证
	public ResultBooleanVO bindBankCardValidate(Long company_id, Long bank_map_id) {
		//
		MConsumerBankMapVO bankMapVO = mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(company_id);
		// 出入金只能是主卡
		if (null == bankMapVO || null == bankMapVO.getId()) {
			log.info("公司没有配置默认银行卡。");
			return new ResultBooleanVO(false, null, "公司没有配置默认银行卡。");
		}
		if (!bankMapVO.getId().equals(bank_map_id)) {
			log.info("绑定的出入金卡不是主卡，不能绑定bankMapVO={},bank_map_id={},bankMapId={}", bankMapVO, bank_map_id);
			return new ResultBooleanVO(false, null, "有主卡存在，不能绑定非主卡的银行卡");
		}
		if (null != bankMapVO.getOut_card_type()) {
			log.info("此卡已经绑定了，不能再次绑定");
			return new ResultBooleanVO(false, null, "此卡已经绑定了，不能再次绑定");
		}
		// 没有默认的主卡，返回true
		return new ResultBooleanVO();
	}

	// 绑卡操作
	private ResultBooleanVO bindBankCardOper(Long company_id, Long bank_map_id) {
		// 获取用户、银行信息
		ResultObjectVO<MCompanyVO> resultVO = mBaseCompanyServiceImpl.search_byIds(new MCompanyForm(company_id));
		if (null == resultVO.getObjVo()) {
			return new ResultBooleanVO("sj.m.company.save.error");
		}
		MCompanyVO companyVO = resultVO.getObjVo();
		MConsumerBankMapVO bankMapVO = null;
		if (null == bank_map_id) {
			bankMapVO = this.mBaseConsumerBankMapServiceImpl.query_consumer_bank_vo(company_id);
		} else {
			bankMapVO = this.mBaseConsumerBankMapServiceImpl.query_consumer_bank_map_by_id(bank_map_id).getObjVo();
		}

//		// 生成绑定出入金form
//		BindBankCardForm cardForm = ClassPackUtil.createBindBankCardFormForCompany(companyVO, bankMapVO, webServiceUtils.queryBranchAccountNoCompany(company_id), webServiceUtils.queryTradeMemberCodeCompany(company_id));
//		try {
//			HttpResult httpResult = httpPoolManager.getHttpClient().postAuthorization(WebServiceUtil.bindBankCardUrl(), ContentType.JSON, null, MConsumerWebServiceImpl.HTTP_TIME_OUT, JackJsonUtil.objToJson(cardForm), "UTF-8");
//			if (null != httpResult && 200 == httpResult.getStatus()) {
//				log.info("绑定出入金结果 httpResult={}", httpResult.getHttpresult());
//				B2BQueryAccountRssultVO accountResult = JackJsonUtil.jsonToJavaBean(httpResult.getHttpresult(), B2BQueryAccountRssultVO.class);
//				if (!accountResult.isSuccess() && !MSystemConstants.HX_RETURN_FALSE_PASS.equals(accountResult.getMsgcode())) {
//					return new ResultBooleanVO(false, "sx.consumer.bank.bind.bank.card.fail", accountResult.getMsg());
//				}
//			}
//		} catch (Exception e) {
//			log.error("绑定出入金账户http异常error={}", e);
//			return new ResultBooleanVO("sx.consumer.bank.bind.bank.card.fail");
//		}
		return new ResultBooleanVO();
	}

	// 解绑操作
	public ResultBooleanVO relieveBankCardOper(Long company_id, MConsumerBankMapVO bankMapVO) {
		// 获取用户、银行信息
		ResultObjectVO<MCompanyVO> resultVO = mBaseCompanyServiceImpl.search_byIds(new MCompanyForm(company_id));
		if (null == resultVO.getObjVo()) {
			return new ResultBooleanVO("sj.m.company.save.error");
		}
		MCompanyVO companyVO = resultVO.getObjVo();

//		// 生成解绑出入金form
//		BindBankCardForm cardForm = ClassPackUtil.createRelieveBankCardFormForCompany(companyVO, bankMapVO, webServiceUtils.queryBranchAccountNoCompany(company_id), webServiceUtils.queryTradeMemberCodeCompany(company_id));
//		try {
//			HttpResult httpResult = httpPoolManager.getHttpClient().postAuthorization(WebServiceUtil.bindBankCardUrl(), ContentType.JSON, null, MConsumerWebServiceImpl.HTTP_TIME_OUT, JackJsonUtil.objToJson(cardForm), "UTF-8");
//			if (null != httpResult && 200 == httpResult.getStatus()) {
//				log.info("解绑出入金结果 httpResult={}", httpResult.getHttpresult());
//				B2BQueryAccountRssultVO accountResult = JackJsonUtil.jsonToJavaBean(httpResult.getHttpresult(), B2BQueryAccountRssultVO.class);
//				if (!accountResult.isSuccess()) {
//					return new ResultBooleanVO(false, "sx.consumer.bank.bind.bank.card.fail", accountResult.getMsg());
//				}
//
//			}
//		} catch (Exception e) {
//			log.error("绑定出入金账户http异常error={}", e);
//			return new ResultBooleanVO(false, null, "解绑卡异常，操作失败");
//		}
		return new ResultBooleanVO();
	}

	/** 批量子账户信息变更 **/
	public ResultBooleanVO accountInfoChangeBatch(Long[] company_ids) {
		if (company_ids.length == 0) {
			return new ResultBooleanVO(false, null, "请选择要操作的公司");
		}
		log.info("子账户信息变更company_ids={}", NumberUtil.arrayToStr(company_ids));
		StringBuilder error = new StringBuilder();
		ResultBooleanVO result = null;
		for (Long company_id : company_ids) {
			MCompanyOperationInfoVO operationVO = mCompanyOperationInfoServiceImpl.queryByCompanyId(company_id).getObjVo();
			MCompanyVO companyVO = mBaseCompanyServiceImpl.search_byIds(new MCompanyForm(company_id)).getObjVo();
			if (!MAccountNumStatus.OPENED.equals(operationVO.getHuaxia02_account_status())) {

				log.info("company_name={}未开户，不能进行变更操作", companyVO.getCompany_name());
				error.append(companyVO.getCompany_name()).append("未开户，不能进行变更操作  \n ");
			} else {
				result = accountInfoChange(companyVO);
				if (!result.isSuccess()) {
					log.info("company_name子账户信息变更失败error={}", companyVO.getCompany_name(), result.getMsg());
					error.append(companyVO.getCompany_name()).append("子账户信息变更失败  \n ");
				}
			}
		}
		if (error.length() > 0) {
			return new ResultBooleanVO(false, null, error.toString());
		}
		return new ResultBooleanVO();
	}

	/**
	 * 子账户信息变更
	 * 
	 * @param consumer_id
	 * @param tradeMemBerCode
	 * @param accountNo
	 * @return
	 */
	private ResultBooleanVO accountInfoChange(MCompanyVO companyVO) {
//		// 数据封装
//		AccInfoChangeForm changeForm = ClassPackUtil.createAccInfoChangeFormForCompany(companyVO, webServiceUtils.queryTradeMemberCodeCompany(companyVO.getId()), webServiceUtils.queryBranchAccountNoCompany(companyVO.getId()));
//		try {
//			HttpResult httpResult = httpPoolManager.getHttpClient().postAuthorization(WebServiceUtil.accountInfoChangeUrl(), ContentType.JSON, null, MConsumerWebServiceImpl.HTTP_TIME_OUT, JackJsonUtil.objToJson(changeForm), "UTF-8");
//			if (null != httpResult && 200 == httpResult.getStatus()) {
//				log.info("子账户信息变更 httpResult={}", httpResult.getHttpresult());
//				B2BchangeAccountVO changeAccountVO = JackJsonUtil.jsonToJavaBean(httpResult.getHttpresult(), B2BchangeAccountVO.class);
//				if (!changeAccountVO.isSuccess()) {
//					return new ResultBooleanVO(false, "sx.consumer.bank.account.info.change", changeAccountVO.getMsg());
//				}
//			}
//		} catch (Exception e) {
//			log.error("子账户信息变更http异常error={}", e);
//			return new ResultBooleanVO("sx.consumer.bank.account.info.change");
//		}
		return new ResultBooleanVO();
	}

//	/**
//	 * 获取子账户信息
//	 * 
//	 * @param consumer_id
//	 * @param tradeMemBerCode
//	 * @param accountNo
//	 * @return
//	 */
//	public B2BQueryAccountVO getAccountInfo(Long company_id) {
//		// 数据封装
//		AccInfoQueryForm queryForm = new AccInfoQueryForm();
//		queryForm.setMemBerCode(webServiceUtils.queryTradeMemberCodeCompany(company_id));
//		queryForm.setSubAccount(webServiceUtils.queryBranchAccountNoCompany(company_id));
//		queryForm.setGradeCode("");
//		try {
//			String url = WebServiceUtil.getAccountInfoUrl();
//			HttpResult httpResult = httpPoolManager.getHttpClient().postAuthorization(url, ContentType.JSON, null, MConsumerWebServiceImpl.HTTP_TIME_OUT, JackJsonUtil.objToJson(queryForm), "UTF-8");
//			if (null != httpResult && 200 == httpResult.getStatus()) {
//				log.info(" 获取子账户信息 httpResult={}", httpResult.getHttpresult());
//				B2BQueryAccountVO statusVO = JackJsonUtil.jsonToJavaBean(httpResult.getHttpresult(), B2BQueryAccountVO.class);
//				if (!statusVO.isSuccess()) {
//					return statusVO;
//				}
//				return statusVO;
//			}
//		} catch (Exception e) {
//			log.error(" 获取子账户信息http异常error={}", e);
//		}
//		return new B2BQueryAccountVO("sx.consumer.bank.account.get.accountInfo");
//	}

}
